﻿using LY.DataAccess.Repository;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace LY.Business.ServiceImpl
{
    public class ScoreService : IScoreService
    {
        ScoreRepository _score;
        public ScoreService()
        {
            _score = new ScoreRepository();
        }
        /// <summary>
        /// 积分分页查询
        /// </summary>
        /// <param name="search"></param>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<U_Score_EX> QueryScoreList(ScoreSearch search, PageView view)
        {
            return _score.QueryScoreList(search, view);
        }
        //增
        public int AddScore(U_Score score)
        {

            return _score.InsertScore(score);
        }
        //删
        public int DeleteScore(int? id)
        {
            return _score.DeleteScore(id);
        }
        //改
        public int SaveScore(U_Score score)
        {
            return _score.UpdateScore(score);
        }
        //查
        public List<U_Score> GetScoreByMemeberId(int? memberid)
        {
            return _score.GetScoreByMemeberId(memberid);
        }
        public U_Score GetScore(int? id)
        {
            return _score.GetScore(id);
        }

        //根据MemberId 获取最新的积分
        public U_Score GetLastScoreByMemberId(int myMemberId)
        {
            return _score.GetLastScoreByMemberId(myMemberId);
        }
        //检验今天是否签到
        public int CheckSignUpToday(int memberId)
        {
            return _score.CheckSignUpToday(memberId);
        }

        //会员积分
        public int GetScoreByMID(int memberId)
        {
            return _score.GetScoreByMID(memberId);
        }


        #region 积分规则


        public int SaveScoreSetting(U_ScoreSetting scoreSetting)
        {
            return _score.SaveScoreSetting(scoreSetting);
        }
        public PagedList<U_ScoreSetting> QueryScoreSettingList(ScoreSearch search, PageView view)
        {
            return _score.QueryScoreSettingList(search, view);
        }
        public U_ScoreSetting GetScoreSetting(int? id)
        {
            return _score.GetScoreSetting(id);
        }
        public U_ScoreSetting GetScoreSetting(string name)
        {
            return _score.GetScoreSetting(name);
        }


        public int SaveScore(string p1, int p2)
        {
            return SaveScore(p1, p2, 0);
        }

        /// <summary>
        /// 新增积分
        /// </summary>
        /// <param name="p1">积分类型 大转盘</param>
        /// <param name="p2">会员ID</param>
        /// <param name="p3">获得积分</param>
        public int SaveScore(string p1, int p2, int p3)
        {
            var setting = GetScoreSetting(p1);
            if (setting != null && setting.extcredits1 != null)
            {
                var score = new U_Score
                {
                    MemberId = p2,
                    CreateTime = DateTime.Now,
                    CreateUser = "system",
                    InCome = p3 == 0 ? (int)setting.extcredits1 : (int)(setting.extcredits1 * setting.extcredits3 * p3),
                    Type = setting.extcredits2,
                    Balance = 0,
                    OutPay = 0,
                    Remark = setting.Name
                };
                AddScore(score);
            }


            return _score.GetScoreByMID(p2);
        }
        #endregion


        #region 签到

        /// <summary>
        /// 检测当天是否登陆
        /// </summary>
        /// <param name="memberid">用户ID</param>
        /// <returns></returns>
        public bool Ischeck(int memberid)
        {
            return _score.Ischeck(memberid);
        }


        public int GetCheckDay(int memberid)
        {
            return _score.GetCheckDay(memberid);
        }

        /// <summary>
        /// 保存签到记录
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public int SaveCheck(U_Check check)
        {
            return _score.SaveCheck(check);
        }

        public PagedList<U_Check> QueryCheckList(CheckSearch search, PageView view)
        {
            return _score.QueryCheckList(search, view);
        }

        public U_Check GetLastCheck(int memberid)
        {
            return _score.GetLastCheck(memberid);
        }
        #endregion

        //抽奖
        public int SavePrize(T_Prize prize, int awardId)
        {
            int ret = -1;
            using (TransactionScope scope = new TransactionScope())
            {
                //扣除积分
                ret = SaveScore("抽奖", prize.MemberId);
                if (prize.Score > 0)
                {
                    ret = SaveScore("大转盘", prize.MemberId, (int)(prize.Score));
                }
                //减数量
                _score.MinAward(awardId);
                //登记奖品
                _score.Insert(prize);
                scope.Complete();
            }

            return ret;
        }
        /// <summary>
        /// 中奖名单
        /// </summary>
        /// <returns></returns>
        public List<T_Prize> GetPrizeList()
        {
            return _score.GetPrizeList();
        }


        public PagedList<T_Prize> MyLottery(PageView view, T_PrizeSearch search)
        {
            return _score.MyLottery(view, search);
        }


        /// <summary>
        /// 大转盘 奖品列表
        /// </summary>
        /// <returns></returns>
        public List<T_Award> GetAwardList()
        {
            return _score.GetAwardList();
        }


        public List<U_Check_EX> QueryCheckList(string mid, int year, int month)
        {
            return _score.QueryCheckList(mid, year, month);
        }
    }
}
